﻿Imports DevExpress.XtraEditors

Public Class frmManageUser
    Dim data As DataTable
    Public Sub loadUser()
        Try
            data = mysql_query("SELECT id_user,username,password,nama_user,IF(`level`='0','Normal User',IF(`level`='1','Administrator','Unknown')) AS 'level' FROM tb_user")
            GridControlList.DataSource = data
            If data.Rows.Count > 0 Then
                SimpleButtonEdit.Enabled = True
                SimpleButtonDelete.Enabled = True
            Else
                SimpleButtonEdit.Enabled = False
                SimpleButtonDelete.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub frmManageUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadUser()
    End Sub

    Private Sub SimpleButtonDelete_Click(sender As Object, e As EventArgs) Handles SimpleButtonDelete.Click
        If XtraMessageBox.Show("Are you sure want to delete this user?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
            Cursor = Cursors.WaitCursor
            Dim id As String = GridViewList.GetFocusedRowCellDisplayText("id_user").ToString
            If id.Equals(localUID) = False Then
                mysql_non_query(String.Format("DELETE FROM tb_user WHERE id_user = '{0}'", id))
                loadUser()
            Else
                MessageBox.Show("Cannot delete user that used to login")
            End If
            
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub SimpleButtonAdd_Click(sender As Object, e As EventArgs) Handles SimpleButtonAdd.Click
        frmManageUser_Form.Dispose()
        frmManageUser_Form.ShowDialog()
    End Sub

    Private Sub SimpleButtonEdit_Click(sender As Object, e As EventArgs) Handles SimpleButtonEdit.Click
        Try
            Dim id As String = GridViewList.GetFocusedRowCellDisplayText("id_user").ToString
            If id.Equals(localUID) = False Then
                frmManageUser_Form.Dispose()
                frmManageUser_Form.is_edit = True
                frmManageUser_Form.id = id
                frmManageUser_Form.ShowDialog()
            Else
                MessageBox.Show("You are not allowed to edit this user." & vbCrLf & "Please use Profile menu")
            End If
            
        Catch ex As Exception
            XtraMessageBox.Show("Please Select One Item For Editing!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub SimpleButtonRefresh_Click(sender As Object, e As EventArgs) Handles SimpleButtonRefresh.Click
        loadUser()
    End Sub
End Class